<?php

class GeoIpController extends ProjectController
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xFFFFFF,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
	
	public function actionIndex(){
	
		$ip = Yii::app()->request->getParam('ip',null);
		$handle = 'GeoIP:'.$ip;
		 
		$data = Yii::app()->cache->get($handle);
		if ($data === false)
		{
			$location = Yii::app()->geoip->lookupLocation($ip);
			if(empty($location)){
				$data = array(
					'ip' => $ip,
					'country' => null,
					'countryName' => null,
					'region' => null,
					'regionName' => null,
					'city' => null,
				);
			}else{
				$data = array(
					'ip' => $ip,
					'country' => $location ->countryCode3,
					'countryName' => $location ->countryName,
					'region' => $location ->region,
					'regionName' => $location ->regionName,
					'city' => $location ->city,
				);
			}
			
		 
			Yii::app()->cache->set($handle, $data, 3600);
			jsonOut($data);
		}
		
		if($ip == null){
			
			jsonOut(array(
				'ip' => $ip,
				'country' => null,
				'countryName' => null,
				'region' => null,
				'regionName' => null,
				'city' => null,
			));
		}
		
	}
	
	
	
	
}
